package com.nf.controller.home;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.OrderInfoDao;
import com.nf.dao.impl.OrderInfoDaoImpl;
import com.nf.util.GsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 编写servlet，实现根据订单ID删除订单
 */
@WebServlet("/api/DeleteOrderByIdServlet.do")
public class DeleteOrderByIdServlet extends BaseServlet {
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader= req.getReader();
        String str=null;
        String jsonParams="";
        while ((str=reader.readLine())!=null){
            jsonParams=jsonParams+str;
        }
        Map<String,String> map= GsonUtil.parseMap(jsonParams);
        int id= Integer.parseInt(map.get("orderId"));

        OrderInfoDao dao=new OrderInfoDaoImpl();
        int r= dao.del(id);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if(r==1){
            out.print(new Gson().toJson(successJson("删除成功")));
        }else{
            out.print(new Gson().toJson(errorJson("删除失败")));
        }
    }
}
